package com.xuyang.server;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.CollectionType;
import org.codehaus.jackson.type.JavaType;

import com.xuyang.utils.MySqlHelper;
import com.xuyang.utils.SqlHelper;

public class GetDataServlet extends HttpServlet{
	private String flag="over";
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setHeader("Content-Type", "charset=utf-8");
		String category=req.getParameter("category");
		category=new String(category.getBytes("ISO-8859-1"),"UTF-8");
		category=URLDecoder.decode(category);
		System.out.println(category);
		String from=req.getParameter("from");
		String to=req.getParameter("to");
		System.out.println(category+from+to);
		ArrayList<BookBean> list=MySqlHelper.getInstance().select("select * from BookStacks where category=? limit "+from+","+to+"", new Object[]{category});
		if (list.size()!=0) {
			ObjectMapper mapper=new ObjectMapper();
			String str=mapper.writeValueAsString(list);
			resp.getWriter().print(str);
			JavaType type=mapper.getTypeFactory().constructParametricType(ArrayList.class, BookBean.class);
			ArrayList<BookBean> list1=mapper.readValue(str, type);
			for (BookBean bookBean : list1) {
				System.out.println(bookBean.getName()+"aa");
			}
		}else {
			//如果没有了则返回数据，提示已经没有了
			resp.getWriter().print(flag);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}
